using System;
using System.Configuration;
using mojoPortal.Web;
using Resources;
using ud_MenuHTML.Business;
using System.Collections.Generic;
using System.Text;

namespace ud_MenuHTML
{
    public partial class MenuHTMLModule : SiteModuleControl
    {
        private MenuHTMLConfiguration config = new MenuHTMLConfiguration();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            BindMenuHTML();
        }

        private void BindMenuHTML()
        {
            DBMenuHTML repository = new DBMenuHTML();
            List<tbl_MenuHTML> list = repository.GetAll(ModuleId);
            StringBuilder sb = new StringBuilder();
            if (config.UseArtisteer)
            {
                sb.Append("<div class='art-nav'><div class='l'></div><div class='r'></div>");
                sb.Append("<ul id='" + ModuleId + "' class='art-menu'>");
            }
            else
            {
                sb.Append("<ul id='" + ModuleId + "' class='menuhtml'>");
            }
            if (list.Count > 0)
            {
                int i = 0;
                if(config.UseIconSetting)
                {
                    foreach (tbl_MenuHTML item in list)
                    {
                        sb.Append("<li class='item" + i + "'>");
                        if (item.ImageUrl != string.Empty)
                        {
                            sb.Append("<img src=" + ImageSiteRoot + "/" +
                                      ConfigurationManager.AppSettings["ArticleImagesFolder"] + item.ImageUrl +
                                      " alt='' />");
                        }
                        sb.Append("<a href='" + item.Link + "' target='" + item.Target +
                                  "' ><span class='l'></span><span class='r'></span><span class='t'>" + item.Name +
                                  "</span></a>");
                        sb.Append("</li>");
                        i++;
                    }    
                }
                else
                {
                    foreach (tbl_MenuHTML item in list)
                    {
                        sb.Append("<li class='item" + i + "'><a href='" + item.Link + "' target='" + item.Target + "' ><span class='l'></span><span class='r'></span><span class='t'>" + item.Name + "</span></a>");
                        sb.Append("</li>");
                        i++;
                    }    
                }
            }
            sb.Append("</ul>");
            if (config.UseArtisteer)
            {
                sb.Append("</div>");
            }
            ltrMenu.Text = sb.ToString();
        }

        protected virtual void PopulateLabels()
        {
            Title1.EditText = MenuHTMLResource.ButtonEdit;
            Title1.EditUrl = SiteRoot + "/MenuHTML/Admin/MenuEdit.aspx";
            lblHeader.Text = config.HeaderText;
            lblFooter.Text = config.FooterText;
        }

        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new MenuHTMLConfiguration(Settings);
            if (!config.CustomCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.CustomCssClass; }
        }
    }
}
